﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DAL
{
    public class Screen
    {
        public static string BuildUrlForProductDetails(string ProductID, string Title, string CateId, string siteId)
        {
            return EnvInfo.MainPublicRoot + siteId + "/c" + CateId + "_p" + ProductID + "/" + ConvertSubject(Title) + ".html";
        }
        public static string BuildUrlForProductByCategory(string Title, string CateId, string siteId)
        {
            return EnvInfo.MainPublicRoot + siteId + "/c" + CateId + "/" + ConvertSubject(Title) + ".html";
        }
        public static string BuildUrlForNewsDetails(string NewsID, string Title, string CateId, string siteId)
        {
            return EnvInfo.MainPublicRoot + siteId + "/g" + CateId + "_n" + NewsID + "/" + ConvertSubject(Title) + ".html";
        }
        public static string BuildUrlForNewsByGroup(string Title, string CateId, string siteId)
        {
            return EnvInfo.MainPublicRoot + siteId + "/g" + CateId + "/" + ConvertSubject(Title) + ".html";
        }
        public static string BuildUrlForNewsStatic(string Title, string NewsID, string siteId)
        {
            return EnvInfo.MainPublicRoot + siteId + "/s" + NewsID + "/" + ConvertSubject(Title) + ".html";
        }       
        public static string ConvertSubject(string Subject)
        {
            Subject = Subject.Trim();
            Subject = Subject.Replace(".", "");          
            Subject = Subject.Replace("  ", " ");
            string cSubject = "";
            string[] AllSubject = Subject.Split(' ');
            int Max = AllSubject.Length;
            if (Max > 20) Max = 20;
            for (int iIndex = 0; iIndex < Max; iIndex++)
            {
                //if (iIndex > 0) cSubject = cSubject ;
                if (iIndex == Max - 1)
                    cSubject = cSubject + AllSubject[iIndex];
                else
                    cSubject = cSubject + AllSubject[iIndex] + "-";
            }

            string Thga = "aáàạảãâấầậẩẫăắằặẳẵ";
            string HoaA = "AÁÀẠẢÃÂẤẦẬẨẪĂẮẰẶẲẴ";
            string Thge = "eéèẹẻẽêếềệểễeeeeee";
            string HoaE = "EÉÈẸẺẼÊẾỀỆỂỄEEEEEE";
            string Thgo = "oóòọỏõôốồộổỗơớờợởỡ";
            string HoaO = "OÓÒỌỎÕÔỐỒỘỔỖƠỚỜỢỞỠ";
            string Thgu = "uúùụủũưứừựửữuuuuuu";
            string HoaU = "UÚÙỤỦŨƯỨỪỰỬỮUUUUUU";
            string Thgi = "iíìịỉĩiiiiiiiiiiii";
            string HoaI = "IÍÌỊỈĨIIIIIIIIIIII";
            string Thgd = "dđðddddddddddddddd";
            string HoaD = "DĐDDDDDDDDDDDDDDDD";
            string Thgy = "yýỳỵỷỹyyyyyyyyyyyy";
            string HoaY = "YÝỲỴỶỸYYYYYYYYYYYY";
            string Khac = "-',~!@#$%^&*{}[];\"<>/:?‘’";

            string[] all = new string[]{
                Thga,HoaA,Thge,HoaE,Thgo,HoaO,Thgu,HoaU,Thgi,HoaI,Thgd,HoaD,Thgy,HoaY,Khac
            };

            for (int iIndexLine = 0; iIndexLine < all.Length; iIndexLine++)
            {
                for (int iColRow = 1; iColRow < all[iIndexLine].Length; iColRow++)
                {
                    cSubject = cSubject.Replace(all[iIndexLine][iColRow], all[iIndexLine][0]);
                }
            }
            cSubject = cSubject.ToLower();
            cSubject = cSubject.Trim();
            if (cSubject.Length > 0)
            {
                while (cSubject.Substring(0, 1).Equals("-"))
                {
                    cSubject = cSubject.Substring(1);
                    if (cSubject.Trim().Length == 0) break;
                }
            }
            return cSubject;
        }           
    }
}
